/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:28 AM
------------------------------------------------------*/
using hou_elearning.Entity;
using hou_elearning.DataAccess;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace hou_elearning.Business
{
    public class Baihoc_SlideBRL
    {
        #region Init
        private static string EX_NOT_EXIST="Không Tồn Tại Baihoc_Slide Này";
		private static string EX_PK_IBAIHOC_SLIDEID_INVALID="PK_iBaihoc_SlideID không hợp lệ";
		private static string EX_FK_IBAIHOCCID_INVALID="FK_iBaihocID không hợp lệ";
		private static string EX_FK_ISLIDEID_INVALID="FK_iSlideID không hợp lệ";
        #endregion
        #region Public Methods
        /// <summary>
        /// Get Baihoc_Slide Theo ID
        /// </summary>
        /// <param name="PK_iBaihoc_SlideID">Int64:Baihoc_Slide ID</param>
        /// <returns>Baihoc_SlideEntity</returns>        
        public static Baihoc_SlideEntity GetOne(Int64 PK_iBaihoc_SlideID)
        {
            
			if(PK_iBaihoc_SlideID<=0)
				throw new Exception(EX_PK_IBAIHOC_SLIDEID_INVALID);
            return Baihoc_SlideDAL.GetOne(PK_iBaihoc_SlideID);
        }
        /// <summary>
        /// Lấy về List các Baihoc_Slide
        /// </summary>
        /// <returns>List Baihoc_SlideEntity:List Baihoc_Slide Cần lấy</returns>
        public static List<Baihoc_SlideEntity> GetAll()
        {
            return Baihoc_SlideDAL.GetAll();
        }
        public static List<Baihoc_SlideEntity> GetByFK_iBaihocID(Int64 FK_iBaihocID)
		{
            if (FK_iBaihocID <= 0)
				throw new Exception(EX_FK_IBAIHOCCID_INVALID);
            return Baihoc_SlideDAL.GetByFK_iBaihocID(FK_iBaihocID);
		}
        public static List<Baihoc_SlideEntity> GetByFK_iSlideID(Int64 FK_iSlideID)
		{
			if(FK_iSlideID<=0)
				throw new Exception(EX_FK_ISLIDEID_INVALID);
			return Baihoc_SlideDAL.GetByFK_iBaihocID(FK_iSlideID);
		}
        public static List<Baihoc_SlideEntity> GetByFK_iBaihocIDAndFK_iSlideID(Int64 FK_iBaihocID, Int64 FK_iSlideID)
        {
            if (FK_iBaihocID <= 0)
                throw new Exception(EX_FK_IBAIHOCCID_INVALID);
            if (FK_iSlideID <= 0)
                throw new Exception(EX_FK_ISLIDEID_INVALID);
            return Baihoc_SlideDAL.GetByFK_iBaihocIDAndFK_iSlideID(FK_iBaihocID, FK_iSlideID);
        }
        /// <summary>
        /// Kiểm tra và thêm mới Baihoc_Slide
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>Int32: ID của Baihoc_Slide Mới Thêm Vào</returns>
        public static Int32 Add(Baihoc_SlideEntity entity)
        {
            checkLogic(entity);
            checkDuplicate(entity, false);
            checkFK(entity);
            return Baihoc_SlideDAL.Add(entity);
        }
        /// <summary>
        /// Kiểm tra và chỉnh sửa Baihoc_Slide
        /// </summary>
        /// <param name="entity">Baihoc_SlideEntity</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Edit(Baihoc_SlideEntity entity)
        {
            checkExist(entity.PK_iBaihoc_SlideID);
            checkLogic(entity);
            checkDuplicate(entity, true);
            checkFK(entity);
            return Baihoc_SlideDAL.Edit(entity);
        }
        /// <summary>
        /// Kiểm tra và xoá Baihoc_Slide
        /// </summary>
        /// <param name="PK_iBaihoc_SlideID">Int64 : PK_iBaihoc_SlideID</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Remove(Int64 PK_iBaihoc_SlideID)
        {
            checkExist(PK_iBaihoc_SlideID);
            return Baihoc_SlideDAL.Remove(PK_iBaihoc_SlideID);
        }
        #endregion
        #region Private Methods
        private static void checkExist(Int64 PK_iBaihoc_SlideID)
        {
            Baihoc_SlideEntity oBaihoc_Slide=Baihoc_SlideDAL.GetOne(PK_iBaihoc_SlideID);
            if(oBaihoc_Slide==null)
                throw new Exception(EX_NOT_EXIST);
        }
        /// <summary>
        /// Kiểm tra logic Entity
        /// </summary>
        /// <param name="entity">Baihoc_SlideEntity: entity</param>
        private static void checkLogic(Baihoc_SlideEntity entity)
        {   
			if (entity.PK_iBaihoc_SlideID < 0)
				throw new Exception(EX_PK_IBAIHOC_SLIDEID_INVALID);
			if (entity.FK_iBaihocID < 0)
				throw new Exception(EX_FK_IBAIHOCCID_INVALID);
			if (entity.FK_iSlideID < 0)
				throw new Exception(EX_FK_ISLIDEID_INVALID);
        }
        /// <summary>
        /// Kiểm tra trùng lặp bản ghi
        /// </summary>
        /// <param name="entity">Baihoc_SlideEntity: Baihoc_SlideEntity</param>
        private static void checkDuplicate(Baihoc_SlideEntity entity,bool checkPK)
        {
            /* 
            Example
            List<Baihoc_SlideEntity> list = Baihoc_SlideDAL.GetAll();
            if (list.Exists(
                delegate(Baihoc_SlideEntity oldEntity)
                {
                    bool result =oldEntity.FIELD.Equals(entity.FIELD, StringComparison.OrdinalIgnoreCase);
                    if(checkPK)
                        result=result && oldEntity.PK_iBaihoc_SlideID != entity.PK_iBaihoc_SlideID;
                    return result;
                }
            ))
            {
                list.Clear();
                throw new Exception(EX_FIELD_EXISTED);
            }
            */
        }
        /// <summary>
        /// Kiểm tra tồn tại khóa ngoại
        /// </summary>
        /// <param name="entity">Baihoc_SlideEntity:entity</param>
        private static void checkFK(Baihoc_SlideEntity entity)
        {            
			BaihocEntity oBaihoc = BaihocBRL.GetOne(entity.FK_iBaihocID);
            if (oBaihoc == null)
			{
				throw new Exception("Không tìm thấy :FK_iBaihocID");
			}
			SlideEntity oSlide = SlideBRL.GetOne(entity.FK_iSlideID);
            if (oSlide == null)
			{
				throw new Exception("Không tìm thấy :FK_iSlideID");
			}
        }
        #endregion
    }
}
